/**
 * Created by Administrator on 2015/1/19.
 */
define(["jquery"],function($){
	var getUID = (function(){
		var uid = 1;
		return function(){
			return uid++;
		}
	})();
	var PREFIX_PARENT_MENU_ID = "parentMenu-"; //父菜单id
	var PREFIX_SUB_MENU_ID = "subMenu-"; //子菜单id
	var PREFIX_ADD_SUB_MENU_BTN = "addSubMenuBtn-"; //添加子菜单按钮
	var PREFIX_REMOVE_PARENT_MENU_BTN = "removeParentMenuBtn-"; //删除父菜单按钮
	var PREFIX_SET_PARENT_MENU_ACTION = "setParentMenuAction-"; //设置父菜单action
	var Obser = $({});
	var data = [
		{
			id:1,
			name : 'xxx',
			pid:'',
			type:'click',
			code:'http://www.baidu.com'
		},
		{
			id:2,
			name : 'xxx',
			pid:'1',
			type:'click',
			code:'http://www.baidu.com'
		},
		{
			id:3,
			name : 'xxx',
			pid:1,
			type:'click',
			code:'http://www.baidu.com'
		}
	];
	//一级菜单最大个数
	var MAX_MENU_COUNT = 3;
	//二级菜单最大个数
	var MAX_SUB_MENU_COUNT = 4;

	function _init(){
		//添加一级菜单
		$("#addMenuBtn").on("click",function(e){
			addMenuDom();
			return false;
		})
		//删除一级菜单
		$("#menuTbody").on("click",".removeParentMenuBtn",function(e){
			var parentContainer = $(e.currentTarget).parents("td");
		})
		//添加子菜单
		$("#menuTbody").on("click",".addSubMenuBtn",function(e){
			var target = $(e.currentTarget);
			var parentContainer = target.parents("td");
			addSubMenuDom(parentContainer)
			return false;
		})
		//删除子菜单
		$("#menuTbody").on("click",".removeSubMenuBtn",function(e){
			var target = $(e.currentTarget);
			removeSubMenuDom(target);
			return false;
		})
	}
	function addMenuDom(){
		var btn = $("#addMenuBtn");
		var count = btn.attr("data-menu");
		count = Number(count) || 0;
		if(count>=MAX_MENU_COUNT){
			alert("一级菜单个数不能超过"+MAX_MENU_COUNT+"个");
			return false;
		}
		count++;
		var html = buildMenuDom(count)
		$("#menuTbody").append(html);
		btn.attr("data-menu",count);
	}
	function removeMenuDom(){

	}
	function addSubMenuDom(parentContainer){
		var container = parentContainer.children(".subMenuContainer");
		var count = container.children().size();
		if(count >= MAX_SUB_MENU_COUNT){
			alert("二级菜单个数不能超过"+MAX_SUB_MENU_COUNT+"个");
			return;
		}
		var html = buildSubMenuDom();
		container.append(html);
	}
	function removeSubMenuDom(removeBtn){
		removeBtn.parent().remove();
	}

	function buildMenuDom(id){
		var html = "";
		html += '<tr id="parentMenuContainer-'+id+'" class="hover">';
		html += '	<td style="border-top:none;">';
		html += '		<div class="parentmenu">';
		html += '			<input id="'+PREFIX_PARENT_MENU_ID+getUID()+'" type="text" class="form-control parentMenuInp textInp" style="display:inline-block;width:300px;">';
		html += '           <a class="setActionBtn" href="javascript:;" style="border-left:0 none" title="设置此菜单动作"><i class="fa fa-pencil"></i> 设置此菜单动作</a>';
		html += '			<a class="addSubMenuBtn" href="javascript:;" data-subMenuCount="0" title="添加子菜单"><i class="fa fa-plus"></i> 添加子菜单</a>';
		html += '			<a class="removeParentMenuBtn" href="javascript:;"><i class="fa fa-remove"></i> 删除此菜单</a>';
		html += '		</div>';
		html += '       <div class="subMenuContainer" class="designer sonmenu"></div>';
		html += '	</td>';
		html += '</tr>';
		return html;
	}
	function buildSubMenuDom(){
		var html = "";
		html += '<div class="subMenuCon">';
		html += ' <input id="'+PREFIX_SUB_MENU_ID+getUID()+'" type="text" class="form-control subMenuInp textInp" style="display:inline-block;width:220px;">';
		html += ' <a class="setActionBtn" href="javascript:;" style="border-left:0 none" title="设置此菜单动作"><i class="fa fa-pencil"></i> 设置此菜单动作</a>';
		html += ' <a class="removeSubMenuBtn" href="javascript:;" title="删除此菜单"><i class="fa fa-remove"></i> 删除此菜单</a>';
		html += '</div>';
		return html;
	}




	function PMenu(config){
		this.config = config || {};
		this.id = this.config.id;
		this.type = this.config.type;
		this.container = this.config.container;
		this.subMenu = [];
		this.MAX_SUB_MENU_COUNT = MAX_SUB_MENU_COUNT;
		//this.render(this.container);
		return this;
	}
	PMenu.prototype = {
		addSubMenu : function(pid){
			if(this.subMenu.length>=this.MAX_SUB_MENU_COUNT){
				console && console.log("子菜单个数不能超过"+this.MAX_SUB_MENU_COUNT+"个");
				return false;
			}
		},
		removeSubMenu : function(){
			var subMenus = this.subMenu;
			var len = subMenus.length;
			for(var i=0; i<len; i++){
				if(subMenus[i][id]){
					this.subMenu.splice(i,1);
					break;
				}
			}
		},
		setAction : function(){
			if(this.subMenu.length>0){
				console && console.log("父菜单设置了子菜单后不能设置菜单动作");
				return false;
			}
		},
		watchSetActionBtn : function(){

		},
		render : function(){
			var html = "";
			var id = this.id;
			html += '<tr id="parentMenuContainer-'+id+'" class="hover">';
			html += '	<td style="border-top:none;">';
			html += '		<div class="parentmenu">';
			html += '			<input data-id="'+id+'" id="'+PREFIX_PARENT_MENU_ID+id+'" type="text" class="form-control parentMenuInp textInp" style="display:inline-block;width:300px;">';
			html += '           <a data-id="'+id+'" class="setActionBtn" href="javascript:;" style="border-left:0 none" title="设置此菜单动作"><i class="fa fa-pencil"></i> 设置此菜单动作</a>';
			html += '			<a data-id="'+id+'" class="addSubMenuBtn" href="javascript:;" title="添加子菜单"><i class="fa fa-plus"></i> 添加子菜单</a>';
			html += '			<a data-id="'+id+'" class="removeParentMenuBtn" href="javascript:;"><i class="fa fa-remove"></i> 删除此菜单</a>';
			html += '		</div>';
			html += '       <div id="subMenuContainer-'+id+'" class="subMenuContainer" class="designer sonmenu"></div>';
			html += '	</td>';
			html += '</tr>';
			this.container.append(html);
			return this;
		}
	}
	function SubMenu(config){
		this.config = config || {};
		this.pid = this.config.pid;
		this.id = this.config.id;
		return this;
	}
	SubMenu.prototype = {
		render : function(){
			var html = "";
			var pid = this.pid;
			var id = this.id;
			html += '<div data-pid="'+pid+'" class="subMenuCon">';
			html += ' <input data-pid="'+pid+'" id="'+PREFIX_SUB_MENU_ID+id+'" type="text" class="form-control subMenuInp textInp" style="display:inline-block;width:220px;">';
			html += ' <a data-pid="'+pid+'" class="setActionBtn" href="javascript:;" style="border-left:0 none" title="设置此菜单动作"><i class="fa fa-pencil"></i> 设置此菜单动作</a>';
			html += ' <a data-pid="'+pid+'" class="removeSubMenuBtn" href="javascript:;" title="删除此菜单"><i class="fa fa-remove"></i> 删除此菜单</a>';
			html += '</div>';
			$("#subMenuContainer-"+pid).append(html);
			return this;
		}
	}

	function Menu(config){
		this.config = config || {};
		this.maxParentMenu = this.config.maxParentMenu || 3;
		this.maxSubMenu = this.config.maxSubMenu || 4;
		this.parentMenuCount = 0;
		this.subMenuCount = 0;
		this.menus = [];
		this.init()
	}
	Menu.prototype = {
		init : function(){
			//添加一级菜单
			var that = this;
			$("#addMenuBtn").on("click",function(e){
				that.creatParentMenu();
				return false;
			})
			//删除一级菜单
			$("#menuTbody").on("click",".removeParentMenuBtn",function(e){
				var target = $(e.currentTarget);
				that.removeParentMenu(target);
				return false;
			})
			//添加子菜单
			$("#menuTbody").on("click",".addSubMenuBtn",function(e){
				var target = $(e.currentTarget);
				var id = target.attr("data-id");
				that.createSubMenu(id);
				return false;
			})
			//删除子菜单
			$("#menuTbody").on("click",".removeSubMenuBtn",function(e){
				var target = $(e.currentTarget);
				that.removeSubMenu(target);
				return false;
			})
		},
		creatParentMenu : function(){
			if(this.parentMenuCount>=this.maxParentMenu){
				alert("一级菜单个数不能超过"+this.maxParentMenu+"个");
				return false;
			}
			var pmenu = new PMenu({
				container : $("#menuTbody"),
				type : "parent",
				id : getUID()
			}).render();
			this.pushMenu(pmenu);
			this.parentMenuCount++;
		},
		createSubMenu : function(pid){
			if(!pid){
				console && console.log("缺省pid");
				return false;
			}
			if(this.subMenuCount>=this.maxSubMenu){
				alert("子菜单个数不能超过"+this.maxSubMenu+"个");
				return false;
			}
			this.subMenuCount++;
			var submenu = new SubMenu({pid:pid,id:getUID()}).render();
			this.pushMenu(submenu);
		},
		removeParentMenu : function(target){
			target.parents("tr").remove();
			this.parentMenuCount--;
		},
		removeSubMenu : function(target){
			target.parent().remove();
			this.subMenuCount--;
		},
		pushMenu : function(menu){
			this.menus.push(menu);
		}
	}



	return Menu;
})





